
import java.io.*;
import java.sql.*;
import java.util.*;

public class mythreads extends Thread {
	private Connection con;
	private Statement stmt;
	private int n_trans = 0;
	private int n_acct = 0;
	public mythreads(int nt, int na){
		try{		
	    con = trans.getConnection();	
		stmt= con.createStatement();
		n_trans = nt;
		n_acct = na;
		}catch (SQLException e){
			//
		} catch (Exception e){
			//
		}
	}
	public void run() {
		ResultSet res;
		int ticket = 0;
		String query;
		System.out.print("thread was created successfully!\n");
		
		while (true) {
			
				Random r = new Random();
				int trans_type = r.nextInt(3) + 1;
				int trans_amount = r.nextInt(100) + 1;
				int trans_accid = r.nextInt(n_acct) + 1;
				/* In this transaction part, let trcounter++, then get the transaction id in ticket
				 * and write the sql query into logfile table;
				 */
				try {
					con.setAutoCommit(false);
					query = "update ticket set trcounter=trcounter+1 ";
					stmt.execute("insert into logfile values(\"" + query
							+ "\")");
					stmt.execute(query);
					res = stmt.executeQuery("select * from ticket");
					res.next();
					ticket = res.getInt(1);
					con.commit();
				} catch (Exception e) {
					System.out.print(e);
					System.out.print("roll back!");
					try {
						con.rollback(); // if not succeed, roll back transaction
					} catch (SQLException re) {
						System.out.println(re.getMessage());
						System.out.print(e);
					}
				}
				if (ticket > n_trans)
					break;
				try {
					con.setAutoCommit(false);
					switch (trans_type) {
					case 1:
						query = "update acct set balance =balance+"
								+ Integer.toString(trans_amount)
								+ " where accountid ="
								+ Integer.toString(trans_accid);
						stmt.execute(query);
						stmt.execute("insert into logfile values(\"" + query
								+ "\")");

						query = "insert into trans values ("
								+ Integer.toString(ticket) + ","
								+ Integer.toString(trans_type) + ","
								+ Integer.toString(trans_accid) + ","
								+ Integer.toString(trans_amount) + ",null)";
						stmt.execute(query);
						stmt.execute("insert into logfile values(\"" + query
								+ "\")");

						break;
					case 2:
						query = "update acct set balance =balance"
								+ Integer.toString(-trans_amount)
								+ " where accountid ="
								+ Integer.toString(trans_accid);
						stmt.execute(query);
						stmt.execute("insert into logfile values(\"" + query
								+ "\")");

						query = "insert into trans values ("
								+ Integer.toString(ticket) + ","
								+ Integer.toString(trans_type) + ","
								+ Integer.toString(trans_accid) + ","
								+ Integer.toString(-trans_amount) + ",null)";
						stmt.execute(query);
						stmt.execute("insert into logfile values(\"" + query
								+ "\")");
						break;
					case 3:
						query = "select * from acct where accountid ="
								+ Integer.toString(trans_accid);
						stmt.execute(query);
						stmt.execute("insert into logfile values(\"" + query
								+ "\")");

						query = "insert into trans values ("
								+ Integer.toString(ticket) + ","
								+ Integer.toString(trans_type) + ","
								+ Integer.toString(trans_accid) + ","
								+ Integer.toString(0) + ",null)";
						stmt.execute(query);
						stmt.execute("insert into logfile values(\"" + query
								+ "\")");

						break;
					}
					con.commit();

				} catch (Exception e) {
					System.out.print(e);
					System.out.print("roll back!");
					try {
						con.rollback(); // if not succeed, roll back transaction
					} catch (SQLException re) {
						System.out.println(re.getMessage());
						System.out.print(e);
					}
				}
				
		}// end while
		
		trans.goon() ;
		
	} //end run()


}
